package com.fairytask.ui.main
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.define.FunDefine;
	import com.fairytask.ui.ArenaUI;
	import com.fairytask.ui.MainSkillUI;
	import com.fairytask.ui.MainUI;
	import com.fairytask.ui.PetsUI;
	import com.fairytask.ui.TaskUI;
	import com.fairytask.ui.WorldMapUI;
	import com.fairytask.ui.base.FrameUI;
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 主界面功能菜单 
	 * @author xujh
	 * 
	 */
	public class FunBtnUI extends FrameUI
	{
		private static var _self		:FunBtnUI;
		//显示
		public var mainMc		:Clip;
		public var peopleBtn 	:Button;	//人物
		public var	petBtn		:Button;	//宠物
		public var packageBtn	:Button;	//背包
		public var taskBtn		:Button;	//任务
		public var gangBtn		:Button;	//帮会
		public var formationBtn:Button;	//阵型
		public var systemBtn	:Button;	//系统
		public var worldMapBtn	:Button;	//世界地图
		
		//功能开启(等级，按钮)
		private var _openArr	:Array = [];
		
		public static function getInstance():FunBtnUI
		{
			if(_self == null)
			{
				_self	=	new FunBtnUI();				
			}
			return _self;
		}
		
		public function FunBtnUI(popUI:Boolean=false)
		{
			super(popUI);
			initArr();
			initEvent();
		}
		
		/**
		 * 初始化功能按钮 
		 * 
		 */
		private function initArr():void
		{
			//peopleBtn.enabled = false;
			petBtn.enabled = false;
			gangBtn.enabled = false;
			formationBtn.enabled = false;
			_openArr[FunDefine.PROFILE]		= "peopleBtn";
			_openArr[FunDefine.PETUPSTAG]	= "petBtn";
			_openArr[FunDefine.GANGS]			= "gangBtn";
			_openArr[FunDefine.BATTLEFORMAT]	= "formationBtn";
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			//按钮点击
			taskBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			petBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			peopleBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			packageBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			formationBtn.addEventListener(MouseEvent.CLICK,onshowUI);
						
			//背包发光
			//model.addEventListener(EffectEvent.PACKAGE_COMPLETE,onFlash);
			
			//世界地图
			worldMapBtn.addEventListener(MouseEvent.CLICK,onshowUI);
		}
		private function onshowUI(evt:MouseEvent):void
		{
			ArenaUI.getInstance().changeHide();
			switch(evt.currentTarget)
			{
				case taskBtn:
					onTask(evt);
					break;
				case petBtn:
					onShowPet(evt);
					break;
				case peopleBtn:
					onShowSkill(evt);
					break;
				case packageBtn:
				case formationBtn:
					onShowPackage(evt);
					break;
				case worldMapBtn:
					WorldMapUI.getInstance().show();
					break;
			}
		}
		
		/**
		 * MainUI背包/阵型界面 
		 * @param evt
		 * 
		 */
		private function onShowPackage(evt:MouseEvent):void
		{
			if(evt.currentTarget == packageBtn)
			{
				isShowUI(MainUI.getInstance(),0);
			}
			else
			{
				isShowUI(MainUI.getInstance(),1);
			}
		}
		
		/**
		 * 任务 
		 * @param evt
		 * 
		 */
		private function onTask(evt:MouseEvent):void
		{
			isShowUI(TaskUI.getInstance());
		}
		
		/**
		 * 伙伴 
		 * @param evt
		 * 
		 */
		private function onShowPet(evt:MouseEvent):void
		{
			isShowUI(PetsUI.getInstance());
		}
		
		/**
		 * 人物 MainSkillUI
		 * @param evt
		 * 
		 */
		private function onShowSkill(evt:MouseEvent):void
		{
			isShowUI(MainSkillUI.getInstance());
		}
		
		/**
		 * 是否打开，打开则关闭，未打开打开 
		 * @param ui
		 * @param pos
		 * 
		 */
		private function isShowUI(ui:FrameUI,pos:int=-1):void
		{
			if(!ui.isOpen(pos))
			{
				ui.showPos(pos);
			}
			else
			{
				ui.hide();
			}
		}
		
		/**
		 * 闪一下 
		 * @param evt
		 * 
		 */
		/*private function onFlash(evt:EffectEvent):void
		{
			TweenMax.to(packageBtn, 0.2, {glowFilter:{color:0xFFFFFF, alpha:0.7, blurX:30, blurY:30, strength:2, quality:1},repeat:1,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0.2, blurX:0, blurY:0, strength:1, quality:1}},onComplete:onFlashOver});
		}*/
		
		private function onFlashOver():void
		{
			packageBtn.filters = null;
		}
		
		/**
		 * 开启按钮特效，播放动画
		 * 
		 */
		public function openBtn(id:int):void
		{
			var mc	:Button = this[_openArr[id]] as Button;
			//慢慢消失
			TweenLite.to(mc,1,{alpha:0,onComplete:onAlpha,onCompleteParams:[mc]});
		}
		
		/**
		 * 消失了可用，慢慢出现 
		 * @param mc
		 * @param val
		 * 
		 */
		private function onAlpha(mc:Button):void
		{
			mc.enabled = true;
			mc.alpha = 0;
			//慢慢出现
			TweenLite.to(mc,1,{alpha:1});
		}
		
		/**
		 * 直接开启功能，无动画 
		 * @param id
		 * 
		 */
		public function initBtn(id:int):void
		{
			this[_openArr[id]].enabled = true;
		}
		
		/**
		 * 重写显示 
		 * @param space
		 * @param onshow
		 * @param argc
		 * 
		 */
		override public function show(space:String=BACKLAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			this.x	=	initX + GameStage.mainLayer.x;
			this.y = 	initY + GameStage.mainLayer.y;
		}
	}
}